<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <th:block th:include="include :: header('数据源列表')"/>
</head>
<body class="pear-container">
<!-- 查询栏 -->
<div class="layui-card">
    <div class="layui-card-body">
        <div class="col-sm-12 search-collapse">
            <form id="formId" class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">数据源：</label>
                    <div class="layui-input-inline">
                        <select name="dataSourceId" lay-verify="required" dict-code="DATA_SOURCE"
                                th:attr="default-value=*{type}">
                        </select>
                    </div>
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="data-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
                        <i class="layui-icon layui-icon-add-1"></i>
                        全库比对
                    </button>
                </div>
            </form>
        </div>
    </div>
    <!-- 表信息 列表 -->
    <div class="layui-col-md3">
        <div class="layui-card">
            <div class="layui-card-body">
                <table class="layui-table" lay-size="sm" id="table-info-table" lay-filter="table-info-table"></table>
            </div>
        </div>
    </div>
    <!-- 字典数据列表 -->
    <div class="layui-col-md9">
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="dict-data-table" lay-filter="dict-data-table"></table>
            </div>
        </div>
    </div>
</div>
</body>

<!-- 表信息 表头操作按钮 -->
<script type="text/html" id="table-info-toolbar">
    <form id="tableInfoFormId" class="layui-form">
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <input type="text" name="tableKey" placeholder="" class="layui-input">
            </div>
            <button class="pear-btn pear-btn-md pear-btn-primary" lay-event="tableQuery">
                <i class="layui-icon layui-icon-search"></i>
            </button>
        </div>
    </form>
</script>
<!-- 表信息 表单操作按钮 -->
<script type="text/html" id="table-info-bar">
    <button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="details">
        <i class="layui-icon layui-icon-transfer"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
        <i class="layui-icon layui-icon-template-1"></i>
    </button>
</script>

<th:block th:include="include :: footer"/>

<script type="text/html" id="type_parseDictText">
    {{layui.dictionary.parseDictTexts(d.type,  'DB_TYPE')}}
</script>

<script>
    layui.use(['table', 'form', 'jquery','dictionary'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;

        let prefix = "/meta/tableManage/";

        let cols = [
            [
                {
                  field: 'tableName',
                  title: '连接名',
                  templet: function (row) {
                    return row.tableName + ' ' + row.tableComment;
                  }
                }, {
                  title: '操作',
                  toolbar: '#table-info-bar',
                  align: 'center', width: 150
                }
            ]
        ]

        let tableInfoList = [];

        table.render({
            elem: '#table-info-table',
            url: prefix + 'selectAllList',
            cols: cols,
            skin: 'line',
            toolbar: '#table-info-toolbar',
            defaultToolbar: [],
            done: function(res, curr, count) {
                tableInfoList = res.data;
            }
        });

        // 最上方查询栏查询
        form.on('submit(data-query)', function (data) {
            table.reload('table-info-table', {where: data.field})
            return false;
        });

        // 表信息 表头查询按钮
        form.on('submit(table-info-tool-query)', function (data) {
            console.log("表头查询---" + data.tableKey);
            let newList = tableInfoList.filter(item => item.tableName.indexOf(data.tableKey) > -1
                                                    || item.tableComment.indexOf(data.tableKey) > -1);
            console.log(newList)
            table.load('table-info-table', {data: newList})
            return false;
        });

        // 表信息 列表操作按钮事件
        table.on('tool(table-info-table)', function (obj) {
            if (obj.event === 'details') {
                console.log("123123");
                console.log(tableInfoList);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'linkTest') {
                window.linkTest(obj);
            }
        });

        form.on('submit(table-info-query)', function (data) {
            table.reload('table-info-table', {where: data.field})
            return false;
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['550px', '500px'],
                content: prefix + 'view'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['550px', '500px'],
                content: prefix + 'view?id=' + obj.data['id']
            });
        }

        window.linkTest = function (obj) {
            $.ajax({
                    url: prefix + "linkTest/" + obj.data['id'],
                    dataType: 'json',
                    type: 'get',
                    success: function (result) {
                        if (result.success) {
                            layer.msg('连接成功', {icon: 1, time: 1000});
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该${functionName}', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('data-table', {where: param});
        }
    })




</script>
</html>